package com.itheima.test;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
 * JFreechart是Java平台上的一个开放的图表绘制类库。
 *
 * @author mewp
 * @date 2022/12/28 10:28
 **/
public class JfreeChartsDemo {
    public static void main(String[] args) throws IOException {
        //pieChart();
        lineChart();
        System.out.println("成功...");
    }

    /**
     * 饼图&3D饼图
     *
     * @throws IOException
     */
    private static void pieChart() throws IOException {
        //构建饼图的数据集
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("销售部", 120);
        dataset.setValue("人事部", 50);
        dataset.setValue("技术部", 180);

        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("华文宋体", Font.BOLD, 20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("华文宋体", Font.BOLD, 15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("华文宋体", Font.BOLD, 15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);
        //参数：标题，数据集，是否开启图列，是否开启工具栏，是否开启url跳转
        //平面
        //JFreeChart chart = ChartFactory.createPieChart("各部门人数", dataset, true, false, false);
        JFreeChart chart = ChartFactory.createPieChart3D("各部门人数", dataset, true, false, false);
        //生成一张图表的图片文件
        ChartUtils.saveChartAsPNG(new File("D:\\tmp\\chart1_3D.png"), chart, 400, 300);
    }

    /**
     * 折线图
     *
     * @throws IOException
     */
    private static void lineChart() throws IOException {
        //构建饼图的数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.setValue(15, "技术部", "2011");
        dataset.setValue(11, "技术部", "2012");
        dataset.setValue(10, "技术部", "2013");
        dataset.setValue(16, "技术部", "2014");

        dataset.setValue(10, "销售部", "2011");
        dataset.setValue(30, "销售部", "2012");
        dataset.setValue(6, "销售部", "2013");
        dataset.setValue(16, "销售部", "2014");

        dataset.setValue(10, "产品部", "2011");
        dataset.setValue(20, "产品部", "2012");
        dataset.setValue(30, "产品部", "2013");
        dataset.setValue(15, "产品部", "2014");

        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("华文宋体", Font.BOLD, 20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("华文宋体", Font.BOLD, 15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("华文宋体", Font.BOLD, 15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);
        //参数：标题，x轴的说明，y轴的说明，数据集
        JFreeChart chart = ChartFactory.createLineChart("入职人数", "年度", "人数", dataset);
        //生成一张图表的图片文件
        ChartUtils.saveChartAsPNG(new File("D:\\tmp\\chart2.png"), chart, 400, 300);
    }

    /**
     * 柱状图（条形图）
     *
     * @throws IOException
     */
    private static void barChart() throws IOException {
        //构建柱状图的数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.setValue(15, "技术部", "2011");
        dataset.setValue(11, "技术部", "2012");
        dataset.setValue(10, "技术部", "2013");
        dataset.setValue(16, "技术部", "2014");

        dataset.setValue(10, "销售部", "2011");
        dataset.setValue(30, "销售部", "2012");
        dataset.setValue(6, "销售部", "2013");
        dataset.setValue(16, "销售部", "2014");

        dataset.setValue(10, "产品部", "2011");
        dataset.setValue(20, "产品部", "2012");
        dataset.setValue(30, "产品部", "2013");
        dataset.setValue(15, "产品部", "2014");

        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("华文宋体", Font.BOLD, 20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("华文宋体", Font.BOLD, 15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("华文宋体", Font.BOLD, 15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);
        //参数：标题，x轴的说明，y轴的说明，数据集
        JFreeChart chart = ChartFactory.createBarChart("入职日期", "年度", "人数", dataset);
        //生成一张图表的图片文件
        ChartUtils.saveChartAsPNG(new File("D:\\tmp\\chart3.png"), chart, 400, 300);
    }
}
